import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/model/login/login_util.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/page/min/min_open_vip_dialog.dart';
import 'package:lcapp/widget/asr_app_bar.dart';
import 'package:webview_flutter/webview_flutter.dart';

class FaxianWebviewPage extends StatefulWidget {
  final Map arguments;
  const FaxianWebviewPage({Key? key, required this.arguments}) : super(key: key);

  @override
  _FaxianWebviewPageState createState() => _FaxianWebviewPageState();
}

class _FaxianWebviewPageState extends State<FaxianWebviewPage> {
  late WebViewController _controller;
  late String title;
  late StreamSubscription _subscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (Platform.isAndroid) {
      // WebView.platform = SurfaceAndroidWebView();
    }
    title = widget.arguments['title'] ?? '';
    print('url====='+widget.arguments['url']);
    /// 监听支付状态
    // _subscription = eventBus.on<PayState>().listen((event) {
    //   if (event.state == 0) {
    //     _controller.runJavascript("setVip(1)");
    //   }
    // });

  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AsrAppBar(
        barColor: Colors.white,
        backgroundColor: defaultBarColor,
        titleText: title ?? '',
        elevation: 0.01,
        leadFun: ()async{
          if(await _controller.canGoBack()){
            _controller.goBack();
          }else{
            Navigator.of(context).pop();
          }
        },
      ),
      body: Container(
        child: WebViewWidget(
          controller: _controller,
          // backgroundColor: Colors.white,
          // javascriptMode: JavascriptMode.unrestricted,
          // initialUrl: widget.arguments["url"],
          // onWebViewCreated: (controller) {
          //   _controller = controller;
          //   _controller.clearCache();
          //   showLoading(maskType: EasyLoadingMaskType.none);
          // },
          // javascriptChannels: <JavascriptChannel>{
          //   JavascriptChannel(
          //       name: "LoginApp",
          //       onMessageReceived: (JavascriptMessage message) {
          //         LoginUtil.login();
          //       }),
          //   JavascriptChannel(
          //       name: "APPOpenVip",
          //       onMessageReceived: (JavascriptMessage message) {
          //         SmartDialog.show(
          //             alignmentTemp: Alignment.bottomCenter,
          //             clickBgDismissTemp: true,
          //             maskColorTemp: Color.fromRGBO(0, 0, 0, 0.75),
          //             widget: GestureDetector(
          //               onTap: (){
          //                 SmartDialog.dismiss();
          //               },
          //               child: MinOpenVipDialogPage(),
          //             )
          //         );
          //       }),
          // },
          // onPageFinished: (url) {
          //   hideLoading();
          // },
          // onWebResourceError: (error) {
          //   hideLoading();
          // },
          // onPageStarted: (str) {},
        ),
      ),
    );
  }
  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    hideLoading();
  }
}
